/*
 * kalib_con.c
 *
 * Created: 2012-08-22 21:55:32
 *  Author: Antrykot
 */ 

#include "../sc-s/main.h"
#include "../sc-s/lcd.h"
#include "../sc-s/sc-s.h"
#include "../sc-s/utils.h"
#include <stdlib.h>
#include "../sc-s/trans-s.h"


void kalib_routin()
{
	
	if (calib_ptr < CM_STNIS)
	{
		calib_ptr = CM_STNIS;
	}
	
	u16 time;
	lcd_home();
	time = TimerH * 64 + (TCNT1 / 1000);
	
	if (!PLU || !MIN || !ENT || !last_time )
	{
		last_time = time;
	}

	if (time - last_time > 180 || time > 480)		// 60s/ 8min time out.
	{
		tp_addr = P_ADDR_RESET_DEFAULTS;
		tk_data = 1;
		tk_addr = K_ADDR_CAL_FINISH;		// finish error
		lcd_print(" !CZAS! ");
		WAITMS(1000);
		lcd_home();
		lcd_print("!KONIEC!");
		WAITMS(3000);
		Pro = PRO_WORK;
		Awp = _read(EEP_AWP);
		return;
	}

	switch (calib_ptr)
	{
		
		
		case CM_STNIS:
		if (calib_ptr_deep)
		{
			cal_low();
			return;
		}
		lcd_print(" KALIB. ");
		if (!ENT)
		{
			calib_ptr_deep = 1;
			//tp_addr = P_ADDR_SW_LRANGE;
			//wait_deliver_P();
			WAITMS(200);
			return;
		}
		break;
		
		
		case CM_FAB:

		lcd_print("UST.FAB ");
		if (!ENT)
		{
			tp_addr = P_ADDR_L_RESET;
			lcd_home();
			lcd_print("ZAPIS...");
			wait_deliver_P();
			//tp_addr = P_ADDR_H_RESET;
			//wait_deliver_P();
			
			calib_ptr++;
			WAITMS(400);

			lcd_home();
			lcd_print("ZAPISANO");

			WAITMS(2000);
			return;
		}
		break;
		
		case CM_WYJ:
		if (calib_ptr_deep)
		{
			cal_out();
			return;
		}

		lcd_print("WYJSCIE ");
		if (!ENT)
		{
			calib_ptr_deep = 1;
			while(!ENT);
			return;
		}
		break;
	}
	
	
	if (!PLU)
	calib_ptr++;
	if (!MIN)
	calib_ptr--;
	
	if (calib_ptr > CM_WYJ)
	calib_ptr = CM_STNIS;
	
	if (calib_ptr < CM_STNIS)
	{
		calib_ptr = CM_WYJ;
	}
	while((!PLU) || (!MIN) || (!ENT));
	
}


#ifdef MEDIA_CO2
#define SZ_PLUS		50		// +- 0.5% CO2
#define SZ_MINUS	50
#define SC_PLUS		80
#define SC_MINS		80

#define GAS_MAX		280
#define GAS_MIN		170

#endif


void cal_low()
{
	char str[8];

	switch (calib_ptr_deep)
	{
		lcd_home();
		
		case 0x01:
		lcd_print(" POD.POW");
		wait_deliver_P();
		tp_addr = P_ADDR_L_RESET;
		wait_deliver_P();
		WAITMS(1500);
		
		calib_ptr_deep++;
		break;
		case 0x02:

		show_media(meas,0x80);		// show spec
		if (!ENT)
		{
			if (meas & 0x1000)
			{
				if ((meas & 0x0FFF) > SZ_MINUS)	// -50ppm
				{
					kalib_serwis(0, (1 << 14) );
					// TODO : Write serwis Z to K
					return;
				}
			}
			else
			{
				if ((meas & 0x0FFF) > SZ_PLUS)	// +50ppm
				{
					kalib_serwis(0, (1 << 14));
					// TODO : Write serwis Z to K
					return;
				}
				
			}
			p_ack = 0;
			tp_addr = P_ADDR_CALLZ;
			
			tk_addr = K_ADDR_LZ;
			// TODO : WRITE ZERO IN K
			
			calib_ptr_deep++;
			lcd_home();
			lcd_print("UST 0.00");
			wait_deliver_P();
			WAITMS(1000);
			lcd_home();
			lcd_print(" ZAPIS! ");
			WAITMS(1000);
		}
		break;
		case 0x03:
		lcd_print(" UST.GAZ");
		WAITMS(1000);
		calib_ptr_deep++;
		
		calib_gas = read_word(EEP_STN);
		break;
		
		case 0x04:
		fuse2(str,calib_gas);
		lcd_print("GAZ:");
		lcd_print(str);
		lcd_print(" ");
		
		if (!PLU)
		{
			calib_gas++;
			if (calib_gas > GAS_MAX)
			calib_gas = GAS_MAX;
		}
		
		if (!MIN)
		{
			calib_gas--;
			if (calib_gas < GAS_MIN)
			calib_gas = GAS_MIN;
		}
		
		if (!ENT)
		{
			calib_ptr_deep++;
			lcd_home();
			lcd_print("POD.GAZ.");
			
			write_word(EEP_STN,calib_gas);
			
			// TODO : WRITE GAS TO P & K
			
			
			WAITMS(1000);
		}
		break;
		
		case 0x05:
		
		show_media(meas,0x80);
		

		
		if (!ENT)
		{
			if (meas & 0x1000)	// 12bit
			{
				kalib_serwis(1,calib_gas | (1 << 14));
				return;
			}
			else
			{
				if (((meas & 0x0FFF) < calib_gas - SC_PLUS) || ((meas & 0x0FFF) > calib_gas + SC_MINS))
				{
					kalib_serwis(1, (1 << 14) );
					return;
				}
			}
			tp_data = calib_gas;
			tp_addr = P_ADDR_CALLS;
			
			tk_data = calib_gas;
			tk_addr = K_ADDR_LS;
			/// TODO: WRITE KALIB OK TO P
			/// TODO: WRITE KALIB OK TO K
			
			lcd_home();
			lcd_print("UST ");
			fuse2(str,calib_gas);
			lcd_print(str);
			lcd_print(" ");
			WAITMS(2000);
			calib_ptr_deep = 0;
			calib_ptr = CM_WYJ;

		}
		
	}
	
	
}



void config_menu()
{
	char str[8];
	
	switch(calib_ptr_deep)
	{
		case 0x00:
		{
			lcd_home();
			lcd_print(" KONFIG ");
			WAITMS(1000);
			lcd_home();
			lcd_print("SET[-/+]");
			WAITMS(1000);
			calib_ptr_deep++;
			break;
		}
		case 0x01:
		{
			lcd_home();
			switch(Ksp)
			{
				case 0:
				lcd_print(" BRAK   ");
				break;
				case 1:
				lcd_print(" KSP-2  ");
				break;
				case 2:
				lcd_print(" KSP-3  ");
				break;
				
			}
			if (!PLU)
			{
				Ksp++;
			}
			
			if (!MIN)
			{
				Ksp--;
			}
			
			if (Ksp > 2)
			Ksp = 0;

			if (!ENT)
			{
				_write(EEP_KSP,Ksp);
				
				if (Ksp == 0 || Ksp == 1)
				{
					tk_data = 0;		// analog
					tk_addr = K_ADDR_KSP;
				}
				else
				{
					tk_data = 1;		// digtrans
					tk_addr = K_ADDR_KSP;
				}
				
				calib_ptr_deep++;
			}
			break;
		}
		case 0x02:
		{
			lcd_home();
			lcd_print("AWP ");
			if (Awp)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("WYL ");
			}
			if (!PLU)
			{
				Awp = 1;
			}
			if (!MIN)
			{
				Awp = 0;
			}
			if (!ENT)
			{
				_write(EEP_AWP,Awp);
				if (Awp)
				{
					calib_ptr_deep += 1;
				}
				else
				{
					calib_ptr_deep = 0x05;	// kcl
				}
			}
			break;
		}
		case 0x03:
		{
			lcd_home();
			lcd_print("AL1=");
			fuse2(str,AL1);
			lcd_print(str);
			lcd_print("  ");
			
			if (!PLU)
			AL1++;
			if (!MIN)
			AL1--;
			
			if (AL1 < 20)
			AL1 = 20;
			if (AL1 > 100)
			AL1 = 100;
			
			if (!ENT)
			{
				calib_ptr_deep++;
				write_word(EEP_AL1,AL1);
				if (AL1 >= AL2)
				{
					AL2 = AL1;
					write_word(EEP_AL2,AL2);
				}
			}
			break;
		}
		case 0x04:
		{
			lcd_home();
			lcd_print("AL2=");
			fuse2(str,AL2);
			lcd_print(str);
			lcd_print("  ");
			
			if (!PLU)
			AL2++;
			if (!MIN)
			AL2--;
			
			if (AL2 < AL1)
			AL2 = AL1;
			if (AL2 > 100)
			AL2 = 100;
			
			if (!ENT)
			{
				calib_ptr_deep++;
				write_word(EEP_AL2,AL2);
			}
			break;
		}
		case 0x05:
		{
			lcd_home();
			lcd_print("KCL ");
			if (Kcl)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("WYL ");
			}
			if (!PLU)
			{
				Kcl = 1;
			}
			if (!MIN)
			{
				Kcl = 0;
			}
			if (!ENT)
			{
				_write(EEP_KCL,Kcl);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x06:
		{
			lcd_home();
			lcd_print("WY-U ");
			if (uout)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("WYL ");
			}
			if (!PLU)
			{
				uout = 1;
			}
			if (!MIN)
			{
				uout = 0;
			}
			if (!ENT)
			{
				_write(EEP_UOUT,uout);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x07:
		{
			lcd_home();
			lcd_print("LCD ");
			if (lcdmode)
			{
				lcd_print("ZAL ");
			}
			else
			{
				lcd_print("TIME");
			}
			if (!PLU)
			{
				lcdmode = 1;
			}
			if (!MIN)
			{
				lcdmode = 0;
			}
			if (!ENT)
			{
				_write(EEP_LCDM,lcdmode);
				calib_ptr_deep += 1;

			}
			break;
		}
		case 0x08:
		{
			lcd_home();
			lcd_print("ZAPISANE");
			WAITMS(2000);
			Pro = PRO_WORK;
			intro();
			break;
		}
	}
	
	if (!PLU || !MIN)
	{
		WAITMS(100);
	}
	
	while( !ENT);
	
}



void calc_dac_out()
{
	if (!uout)
	{
		napb = 0;
		napa = 0;
		return;
	}
	if (uout == 2)	// in calib;
	return;
	
	u16 tmp = meas;
	float fnap;
	
	
	if (tmp & 0x4000)	// 14bit
	{
		tmp &= 0x3FFFF;
		
		if (tmp & 0x1000)		// 12bit  - minus
		{
			tmp &= 0x0FFF;
			
			if (tmp < 20)		// -0.2%
			{
				napb = U2_40;
				napa = U1_40;
				return;
			}
			napa = U1_35;
			napb = U2_35;
			return;
		}
		
		
		fnap = U2_20 - U2_40;
		fnap = fnap / 10000 * tmp + U2_40;
		napb = fnap;
		
		tmp = meas & 0x0FFF;
		
		if (tmp > 500)
		{
			napa = U1_21;
			return;
		}

		
		fnap = ( U1_20 - U1_40 ) / 500;
		fnap = fnap * tmp ;
		napa = fnap + U1_40;
	}
	
	
}